<--- %%NOBANNER%% --> _ranbeta.sas
 BackForward

/*-------------------<-- Start of Description -->--------------------\
| Generate random variate from a normal distribution                 |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--- Start of Files or Arguments Needed -->-----------|
| Arguments Need:                                                    |
|      seed  - seed; Required, default is the current system time;   |
|      var   - the variable to save the generated random variates;   |
|      alpha - alpha;                                                |
|      beta  - beta;                                                 |
|      Note: mean=a/(a+b), variance=a*b/(a+b)^2*(a+b+1);             |
|      temp  - the temporary variable to update the seed;            |
|             default is _rannor0_;                                  |
|---------------<--- End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
|Example                                                             |
|   data one;                                                        |
|      do i=1 to 20;                                                 |
|      x=%_ranbeta(seed=1, alpha=2, beta=2);                         |
|      %_ranbeta(var=y, seed=12, alpha=2, beta=2);                   |
|      output;                                                       |
|      end;                                                          |
|   run; %print(one);                                                |
|Usage: %_ranbeta(seed=%sysfunc(datetime(), 15.), alpha=REQUIRED,    |
|                 beta=REQUIRED, var=, temp=_ranbeta0_);             |
\-------------------<--- End of Example and Usage -->---------------*/
%macro _ranbeta(seed=%sysfunc(datetime(), 15.), alpha=REQUIRED, 
                beta=REQUIRED, var=);
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 3-22-2002 6:30pm;                  |
| Purpose: Random Beta Generator;             |
\--------------------------------------------*/
%local nseed seed1 seed2;
%let _achk_=%sysfunc(rxmatch(%sysfunc(rxparse($a|$A|$w)),&alpha));
%let _bchk_=%sysfunc(rxmatch(%sysfunc(rxparse($a|$A|$w)),&beta));
%if (%quote(&seed) eq) or &_achk_ or &_bchk_ %then %do;
   %if (%quote(&seed) eq) %then %do;
      %put ==> Error: This is not a valid seed!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %if &_achk_ %then %do;
      %put ==> Error: &alpha is not a valid alpha!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %if &_bchk_ %then %do;
      %put ==> Error: &beta is not a valid beta!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %goto finish;
%end;
%else %do;
   %let nseed=1; %let seed1=%qscan(&seed, &nseed, %str( ())); 
   %do %while(%length(&&seed&nseed));
      %put seed&nseed is &&seed&nseed;
      %let nseed=%eval(&nseed+1);
      %let seed&nseed=%qscan(&seed, &nseed, %str( ()));
   %end;
   %let nseed=%eval(&nseed-1);
   %if (&nseed<2) and (not %sysfunc(rxmatch(%sysfunc(rxparse(.|_|$a|$A|$w)),&seed))) %then %do;
      %if %length(&seed) ge 2 %then %do;
	      %let seed1=%substr(&seed, 1, %eval(%length(&seed)/2));
	      %let seed2=%substr(&seed, %eval(%eval(%length(&seed)/2)+1), %eval(%length(&seed)-%eval(%length(&seed)/2)));
      %end;
      %else %do;
         %let seed1=&seed; %let seed2=&seed;
      %end;
   %end;
   %else %if (&nseed>=2) %then;
   %else %do;
      %let seed1=substr(&seed, 1, floor(length(&seed)/2)); 
      %let seed2=substr(&seed, (floor(length(&seed)/2)+1), (length(&seed)-(floor(length(&seed)/2))));
   %end;
   %if (%length(&var)) %then %do;
      drop _tmp_&var;
      %_rangam(seed=&seed1, alpha=&alpha, var=&var); 
      %_rangam(seed=&seed2, alpha=&beta,  var=_tmp_&var);
      &var=&var/(&var+_tmp_&var);
   %end;
   %else 1/(1+%_rangam(seed=&seed1, alpha=&beta)/%_rangam(seed=&seed2, alpha=&alpha));
%end;
%finish:
%mend _ranbeta;